package com.controller;

import com.bean.Message;
import com.mvc.ResponseBody;
import com.service.AdminService;
import com.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;


public class AdminController {
    @ResponseBody("/admin/login.do")
    public String login(HttpServletRequest request, HttpServletResponse response) {
        //System.out.println(request.getRequestURI());
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        AdminService adminService = new AdminService();
        boolean result = adminService.login(username, password);
        Message msg = new Message();
        if (result) {
            msg.setStatus(0);
            msg.setResult("登录成功");
            Date date = new Date();
            String ip = request.getRemoteAddr();
            adminService.updateLoginTime(username, date, ip);
            request.getSession().setAttribute("adminUserName", username);
        } else {
            msg.setStatus(-1);
            msg.setResult("登录失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }
}
